package com.example.plan.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName DjiCloudApiService
 * @Description TODO
 * @Author renhong~Deng
 * @Date 2025/4/9 11:35
 * @Version 1.0
 */

@Service
public class DjiCloudApiService {

    private static final String API_KEY = "1a2123f6c28a8be6dae13fb1013b9cb"; // 替换为你的API Key
    private static final String BASE_URL = "http://localhost:6789";

    private final ObjectMapper objectMapper = new ObjectMapper();

    public String getDroneData() throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet request = new HttpGet(BASE_URL + "/drone/data");
        request.addHeader("X-DJIAPI-KEY", API_KEY);

        try (CloseableHttpResponse response = httpClient.execute(request)) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity);
            }
        }
        return null;
    }

    public String createWaypointMission(List<Waypoint> waypoints) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost request = new HttpPost(BASE_URL + "/mission/waypoint/create");
        request.addHeader("X-DJIAPI-KEY", API_KEY);
        request.addHeader("Content-Type", "application/json");

        WaypointMissionRequest missionRequest = new WaypointMissionRequest();
        missionRequest.setWaypoints(waypoints);
        missionRequest.setAutoFlightSpeed(10f);
        missionRequest.setMaxFlightSpeed(15f);
        missionRequest.setExitMissionOnRCSignalLostEnabled(false);
        missionRequest.setFlightPathMode("NORMAL");
        missionRequest.setHeadingMode("AUTO");
        missionRequest.setFinishedAction("NO_ACTION");

        String jsonRequest = objectMapper.writeValueAsString(missionRequest);
        request.setEntity(new StringEntity(jsonRequest));

        try (CloseableHttpResponse response = httpClient.execute(request)) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity);
            }
        }
        return null;
    }

    public String startWaypointMission(String missionId) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost request = new HttpPost(BASE_URL + "/api/v1/mission/waypoint/start/" + missionId);
        request.addHeader("X-DJIAPI-KEY", API_KEY);
        request.addHeader("Content-Type", "application/json");

        try (CloseableHttpResponse response = httpClient.execute(request)) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity);
            }
        }
        return null;
    }

    // 可以添加其他API调用方法
@Data
    public static class Waypoint {
        private double latitude;
        private double longitude;
        private float altitude;
    }

    @Data
    public static class WaypointMissionRequest {
        private List<Waypoint> waypoints;
        private float autoFlightSpeed;
        private float maxFlightSpeed;
        private boolean exitMissionOnRCSignalLostEnabled;
        private String flightPathMode;
        private String headingMode;
        private String finishedAction;

    }
}
